package com.geekchique.runningbuddy.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.Menu.Item;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.activity.stopclock.StopClockThread;
import com.geekchique.runningbuddy.controller.IOptionsMenu;
import com.geekchique.runningbuddy.controller.IStopClockController;
import com.geekchique.runningbuddy.controller.OptionsMenuImpl;
import com.geekchique.runningbuddy.controller.StopClockControllerImpl;
import com.geekchique.runningbuddy.service.TimeRunnerService;

public class StopClock extends Activity {
    
    protected StopClockThread timerRunnerThread;

	private IStopClockController stopClockController;
	private IOptionsMenu optionsMenu;
	
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);        
        setContentView(R.layout.stopclock);
		optionsMenu = new OptionsMenuImpl(this);
        
        stopClockController = new StopClockControllerImpl(this);
		stopClockController.createStopClockView(this, (TextSwitcher) findViewById(R.id.stopclock_time), (Button) findViewById(R.id.stopclock_buttonstart) );
		stopClockController.createStopClockTargetView(
				this,
				(CheckBox) findViewById(R.id.checkbox_target),
				(TextView) findViewById(R.id.hours),
				(TextView) findViewById(R.id.minutes),
				(TextView) findViewById(R.id.seconds),
				(TextView) findViewById(R.id.milliseconds),
				(Button) findViewById(R.id.hours_up),
				(Button) findViewById(R.id.hours_down),
				(TextView) findViewById(R.id.hours_sep),
				(Button) findViewById(R.id.minutes_up),
				(Button) findViewById(R.id.minutes_down),
				(TextView) findViewById(R.id.minutes_sep),
				(Button) findViewById(R.id.secs_up),
				(Button) findViewById(R.id.secs_down),
				(TextView) findViewById(R.id.seconds_sep),
				(Button) findViewById(R.id.millisecs_up),
				(Button) findViewById(R.id.millisecs_down)
		);;
	}
    
	public void startStopClockService() {
		startService(new Intent(StopClock.this, TimeRunnerService.class), null);
	}

	public void stopStopClockService() {
		stopService(new Intent(StopClock.this, TimeRunnerService.class));
	}

	public Context getContext() {
		return this;
	}	
	
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		optionsMenu.addAllActivitiesToMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(Item item) {
		optionsMenu.selectItem(item);
	    return super.onOptionsItemSelected(item);
	}

}
